package com.ruoyi.system.service.shopping.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.shopping.JwtUtil;
import com.ruoyi.system.domain.shopping.Area;
import com.ruoyi.system.domain.shopping.User;
import com.ruoyi.system.mapper.shopping.AreaMapper;
import com.ruoyi.system.mapper.shopping.UserMapper;
import com.ruoyi.system.service.shopping.AreaService;
import com.ruoyi.system.service.shopping.UserService;
import com.ruoyi.system.view.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User selectUser(Integer userId) {
        return userMapper.selectUser(userId);
    }

    @Override
    public Result login(User user) {
        if (user.getUsername() == null || user.getPassword() == null || user.getUsername() == "" || user.getPassword() == "") {
            return Result.error(null, 201,"账号或密码未填写");
        }
        //查询用户
        User sUser = userMapper.selectUserByUsername(user.getUsername());
        if (sUser == null) {
            return Result.error(null, 201,"用户不存在");
        }
        //判断用户是否被启用
        if(!sUser.getState().equals("0")){
            return Result.error(null,201, "用户异常");
        }
        //判断密码是否相同
        if (sUser.getPassword().equals(user.getPassword())) {
            //相同
            Map<String, String> map = new HashMap<>();
            map.put("userId",sUser.getUserId().toString());
            map.put("username",sUser.getUsername());
            map.put("password",sUser.getPassword());
            String token = JwtUtil.getToken(map,1000000L);
            return Result.success(token, "登陆成功");
        } else {
            return Result.error(null, 201,"密码有误");
        }
    }

    @Override
    public Result register(User user) {
        if (user.getUsername() == null || user.getPassword() == null || user.getUsername() == "" || user.getPassword() == "") {
            return Result.error(null, 201,"账号或密码未填写");
        }
        if (user.getPassword().length() > 16) {
            return Result.error(null, 201,"密码过长");
        }
        if (user.getPassword().length() < 6) {
            return Result.error(null,201, "密码过短");
        }
        User sUser = userMapper.selectUserByUsername(user.getUsername());
        if (sUser != null) {
            return Result.error(null,50020, "用户名存在");
        } else {
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            int i = userMapper.insertUser(user);
            return Result.success(null, i>0 ? "注册成功" : "注册失败");
        }
    }

    @Override
    public List<User> selectUserList(User user) {
        return userMapper.selectUserList(user);
    }

    @Override
    public Boolean updateUserStatus(User user) {
        return userMapper.updateUserStatus(user)>0;
    }


}
